// pages/gangManage/productPassage/productPassage.js
// 自有商品才有修改宣传段子的功能 官方商品由供货商统一修改
const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')

Page({
  data: {
    passage: []
  },


  onLoad: function(option) {
    var that = this
    var userInfo = wx.getStorageSync('userInfo')
    that.setData({
      product_id: option.product_id,
      userInfo: userInfo
    })

    http.post({
      url: "product/getPassage",
      product_id: that.data.product_id
    }, function(data) {
      console.log('段子', data)
      that.setData({
        passage: data
      })
    })
  },

  bindTextAreaBlur: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var passage = that.data.passage
    passage[index].content = e.detail.value
    that.setData({
      passage: passage
    })
  },
  deletePassage(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var passage = that.data.passage
    passage.splice(index, 1)
    that.setData({
      passage: passage
    })
  },
  addSpecifications(e) {
    var that = this
    var passage = that.data.passage
    passage.push({
      content: '',
      gang_id: that.data.userInfo.gangId
    })
    that.setData({
      passage: passage
    })
  },
  formSubmit: function(e) {
    var that = this
    console.log(e.detail.value)
    var gangPassage = []
    for (var d of that.data.passage) {
      if (d.gang_id)
        gangPassage.push(d)
    }
    http.post({
      url: "product/editPassage",
      product_id: that.data.product_id,
      passage: gangPassage
    }, function(data) {
      util.showSuccess('修改成功', function() {
        var pages = getCurrentPages();
        if (pages.length > 1) {
          var prePage = pages[pages.length - 2];
          prePage.setPassageCount(that.data.passage.length)
        }
        wx.navigateBack({
          delta: 1
        })
      })

    })
  }
})